package com.am.mosl.cityandroid.leftmenu;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.activity.BaseActivity;
import com.am.mosl.cityandroid.bean.AccountBean;
import com.am.mosl.cityandroid.constant.HttpConstant;
import com.am.mosl.cityandroid.message.LoginMessage;
import com.am.mosl.cityandroid.util.SettingUtils;

import de.greenrobot.event.EventBus;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author 邓思宇 登入界面
 * 
 */
public class JoinActivity extends BaseActivity {

	private EditText name;
	private EditText phone;
	private EditText number;
	private EditText other;
	private Button join;
	private String pid = "";// 获取的文章ID
	private String token = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.z_party_activity);

		name = (EditText) findViewById(R.id.pname);
		phone = (EditText) findViewById(R.id.pphone);
		number = (EditText) findViewById(R.id.pnumber);
		other = (EditText) findViewById(R.id.pother);
		join = (Button) findViewById(R.id.join_in);

		if (SettingUtils.IsLogin(this)) {
			AccountBean accountBean = new AccountBean();
			accountBean = SettingUtils.getUserInfo(this);
			token = accountBean.getToken();
			String telephone = accountBean.data.getPhone();
			phone.setText(telephone);

		}
		pid = getIntent().getStringExtra("contentId");
		join.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				String pname = name.getText().toString().trim();
				String pphone = phone.getText().toString().trim();
				String pnumber = number.getText().toString().trim();
				String pother = other.getText().toString().trim();

				if (pname.equals("")) {
					Toast.makeText(JoinActivity.this, "姓名不能为空",
							Toast.LENGTH_SHORT).show();
					return;
				}

				if (pphone.equals("")) {
					Toast.makeText(JoinActivity.this, "手机号不能为空",
							Toast.LENGTH_SHORT).show();
					return;
				}

				if (pnumber.equals("")) {
					Toast.makeText(JoinActivity.this, "人数不能为空",
							Toast.LENGTH_SHORT).show();
					return;
				}

				if (!isMobile(pphone)) {
					Toast.makeText(JoinActivity.this, "请输入正确的手机号",
							Toast.LENGTH_SHORT).show();
					return;
				}

				Join(pid, pname, pphone, pnumber, pother);

			}
		});

	}

	protected void Join(String pid2, String pname, String pphone,
			String pnumber, String pother) {

		if (isNetworkAvailable(JoinActivity.this)) {
			FinalHttp finalHttp = new FinalHttp();
			AjaxParams params = new AjaxParams();
			params.put("contentid", pid2);
			params.put("name", pname);
			if (SettingUtils.IsLogin(JoinActivity.this)) {
				params.put("token", token);
			}
			params.put("telephone", pphone);
			params.put("number", pnumber);
			params.put("remark", pother);

			finalHttp.post(HttpConstant.UserJoinHttp, params,
					new AjaxCallBack<Object>() {
						@Override
						public boolean isProgress() {
							return super.isProgress();
						}

						@Override
						public void onSuccess(Object o) {
							super.onSuccess(o);
							try {
								JSONObject jsonObject = new JSONObject(o
										.toString());
								String status = (String) jsonObject
										.opt("status");
								String msg = (String) jsonObject.opt("msg");

								if (status.equals("1")) {
									Toast.makeText(JoinActivity.this,
											msg, Toast.LENGTH_SHORT)
											.show();
									EventBus.getDefault().post(new JoinMessage());
									JoinActivity.this.finish();

								}
								
								else if (status.equals("2")) {

									SettingUtils
											.RemoveAccount(JoinActivity.this);
									LoginMessage message = new LoginMessage();
									message.setType(LoginMessage.QUIT);
									EventBus.getDefault().post(message);

									Toast.makeText(JoinActivity.this,
											"帐号验证失败，请重新登录", Toast.LENGTH_SHORT)
											.show();
									JoinActivity.this.finish();
								}
								
								else {

									Toast.makeText(JoinActivity.this,
											"报名失败：" + msg, Toast.LENGTH_SHORT)
											.show();

									return;

								}

							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					});

		} else {
			Toast.makeText(JoinActivity.this, "网络未连接", Toast.LENGTH_SHORT)
					.show();
		}

	}

	// 点击返回
	public void BacktoBack(View view) {
		this.finish();
	}

    public static class JoinMessage{

        public boolean isSuccess;
    }

	/**
	 * 判断网络是否可用
	 * 
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager mgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] info = mgr.getAllNetworkInfo();
		if (info != null) {
			for (int i = 0; i < info.length; i++) {
				if (info[i].getState() == NetworkInfo.State.CONNECTED) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 手机号验证
	 * 
	 * @param str
	 * @return 验证通过返回true
	 */
	public static boolean isMobile(String str) {
		Pattern p = null;
		Matcher m = null;
		boolean b = false;
		p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
		m = p.matcher(str);
		b = m.matches();
		return b;
	}

}